package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.OmcSafeLogDto;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcSafeLog;
import com.csun.cmny.provider.model.enums.UacRoleCodeEnum;
import com.csun.cmny.provider.model.vo.RpcRoleVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcSafeLogService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-08 14:15
 * @Description: the class omc safe log controller
 */
@RestController
@RequestMapping(value = "/safeLog", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcSafeLogController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcSafeLogService omcSafeLogService;
    @Resource
    private UacRpcService uacRpcService;

    private boolean checkServiceMinisterRole(LoginAuthDto loginAuthDto) {

        // 获取当前用户角色
        List<RpcRoleVo> roleVoList = uacRpcService.queryRoleListByUserId(loginAuthDto.getUserId());

        // 校验用户是否为系统管理员
        for (RpcRoleVo r : roleVoList) {
            if (r.getRoleCode().equals(UacRoleCodeEnum.SERVICE_MINISTER.getCode())) {
                return true;
            }
        }

        return false;
    }

    /**
     * 根据日期查询安全日志
     *
     * @param date  日期
     *
     * @return  the wrapper.
     */
    @PostMapping(value = "/showByDate/{date}")
    public Wrapper showByDate(@PathVariable String date) {

        LoginAuthDto loginAuthDto = getLoginAuthDto();
        // 判断是否服务部长
        if (!checkServiceMinisterRole(loginAuthDto)) {
            // 不是管理员, 无操作权限
            new OmcBizException(ErrorCodeEnum.UAC10012017);
        }

        OmcSafeLog omcSafeLog = new OmcSafeLog();
        omcSafeLog.setDate(date);
        omcSafeLog.setGroupId(loginAuthDto.getGroupId());

        omcSafeLog = omcSafeLogService.selectOne(omcSafeLog);

        //return WrapMapper.ok(omcSafeLog);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),omcSafeLog);
    }

    /**
     * 保存安全日志
     *
     * @param omcSafeLogDto 安全日志
     * @param br            校验结果
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @PostMapping(value = "/save")
    public Wrapper save(@Validated @RequestBody OmcSafeLogDto omcSafeLogDto, BindingResult br) {

        LoginAuthDto loginAuthDto = getLoginAuthDto();
        // 判断是否服务部长
        if (!checkServiceMinisterRole(loginAuthDto)) {
            // 不是管理员, 无操作权限
            new OmcBizException(ErrorCodeEnum.UAC10012017);
        }

        Integer result = omcSafeLogService.saveSafeLog(omcSafeLogDto, loginAuthDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

}
